import serial, time

class SerSrv:
    def __init__(self, port, bps, handler):
        self.ser = serial.Serial(port, bps)
        self.handler = handler

    def run(self):
        while (True):
            '''
            self.ser.write('111'.encode('utf8'))
            content = self.ser.read()
            print('recv:', content)
            '''
            print("readline  ...")
            content = self.ser.readline()
            #content = self.ser.read()
            print('recv -->', content)
            if content:
                have_next = self.handler.handle(self.ser.write, str(content))
                if have_next:
                    time.sleep(10)
                    rsp = self.handler.gen_next_msg()
                    rsp_asc = rsp.encode('ascii')
                    print("response: %s\n" % (rsp_asc,))
                    self.ser.write(rsp_asc)
            

 